using Bridges;
using Microsoft.Web.WebView2.Core;

namespace MSEAPPC
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private async void InitializeWebView2Async() {
            if (this.webView21.CoreWebView2 == null)
            {
                await this.webView21.EnsureCoreWebView2Async();
                //this.webView21.CoreWebView2.OpenDevToolsWindow();
                //await this.webView21.CoreWebView2.ExecuteScriptAsync($"alert('is not safe, try an https link')");
                this.webView21.NavigationCompleted += async (sender, args) =>
                {
                    //MessageBox.Show("lkasjfd");
                    //await this.webView21.CoreWebView2.ExecuteScriptAsync($"alert('is not safe, try an https link')");
                    await this.webView21.CoreWebView2.ExecuteScriptAsync("window.addEventListener('contextmenu', window => {window.preventDefault();});");
                };
            }
        }

        private async void Main_Shown(object sender, EventArgs e)
        {
            Loading loading = new();
            loading.ShowDialog();
            this.ShowInTaskbar = true;
            this.WindowState = FormWindowState.Maximized;
            //跨域
            var path = AppDomain.CurrentDomain.BaseDirectory + "WebViewCache";
            //var path = System.Environment.UserName + "WebViewCache";
            var env = await CoreWebView2Environment.CreateAsync(userDataFolder: path,
                  browserExecutableFolder: null,
                  options: new CoreWebView2EnvironmentOptions($"-disable-web-security --user-data-dir={Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ChromeDevSession")}"));
            await this.webView21.EnsureCoreWebView2Async(env);

            this.webView21.Source = new Uri(Path.Combine(Directory.GetCurrentDirectory(), "dist", "index.html"));
            //this.webView21.Source = new Uri("http://192.168.3.103:9000/");
            this.webView21.CoreWebView2.AddHostObjectToScript("init", new Init());
            this.webView21.CoreWebView2.AddHostObjectToScript("user", new User());
            this.webView21.CoreWebView2.AddHostObjectToScript("admin", new Admin());
            this.webView21.CoreWebView2.AddHostObjectToScript("project", new Project());
            this.webView21.CoreWebView2.AddHostObjectToScript("parts", new Parts());

            //this.webView21.CoreWebView2.OpenDevToolsWindow();
        }
    }
}